package com.google.com.coma.tester;

import com.google.code.coma.domain.Computer;
import com.google.code.coma.domain.LifeCycleStep;
import com.google.code.coma.domain.Part;
import com.google.code.coma.domain.Type;
import org.apache.log4j.Logger;

/**
 */
public class CompletenessTester implements Tester {
	private static final Logger log = Logger.getLogger(CompletenessTester.class);

	@Override
	public void testComputer(Computer computer, String workerId) {
		boolean hasCpu = false, hasMainboard = false;
		int ramCount = 0;

		for (Part part : computer.getParts()) {
			if (part.getType().equals(Type.CPU)) {
				hasCpu = true;
			} else if (part.getType().equals(Type.Mainboard)) {
				hasMainboard = true;
			} else if (part.getType().equals(Type.RAM)) {
				ramCount++;
			}
		}

		if (hasCpu && hasMainboard && (ramCount == 1 || ramCount == 2 || ramCount == 4)) {
			if (log.isInfoEnabled()) {
				log.info("Computer " + computer + "is complete, go further!");
			}
			computer.changeState(LifeCycleStep.TESTED_COMPLETENESS, workerId);
		} else {
			if (log.isInfoEnabled()) {
				log.info("Computer " + computer + "is incomplete, rejecting!");
			}
			computer.changeState(LifeCycleStep.REJECTED, workerId);
		}

	}
}
